package IO;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class CompressionAndDecompressionDemo {
    public static void main(String[] args) {
    //    compression("c:\\test.zip",new File("C:\\Users\\Administrator\\Desktop\\File"));
        //解压
        Decompression("c:\\test.zip","C:\\Users\\Administrator\\Desktop\\File\\");
    }
    private static void compression(String zipFilename, File targetFile){
        //压缩
        System.out.println("正在压缩。。。");
        try {
            //要生成的压缩文件
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
            BufferedOutputStream bos = new BufferedOutputStream(out);
            zip(out,targetFile,bos,targetFile.getName());
            bos.close();
            out.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("压缩完成");

    }
    //ZIP
    private static void zip(ZipOutputStream Zout, File targetFile, BufferedOutputStream bos, String name) throws IOException {
      //如果是个目录
       if(targetFile.isDirectory()){
           File[] files = targetFile.listFiles();
           if(files.length==0){
               //空文件夹
               Zout.putNextEntry(new ZipEntry(name+"/"));//处理空目录
           }
           for(File f:files){//递归处理
               zip(Zout,f,bos,name+"/"+f.getName());
           }
       }else{
           Zout.putNextEntry(new ZipEntry(name));
           InputStream in = new FileInputStream(targetFile);
            BufferedInputStream bis = new BufferedInputStream(in);
            byte[] bytes = new byte[1024];
            int len = -1;
            while((len =bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
            bis.close();

       }

    }

    private static void Decompression(String targetFileName,String parent){
        //解压
        try {
            ZipInputStream zis = new ZipInputStream(new FileInputStream(targetFileName));
            ZipEntry entry = null;
            File file = null;
            while((entry =zis.getNextEntry())!=null && !entry.isDirectory()){
                     file = new File(parent,entry.getName());
                     if(!file.exists()){ //不存在文件，就创建目录
                         new File(file.getParent()).mkdirs();//创建此文件的上级目录
                     }
                     OutputStream fos = new FileOutputStream(file);
                     BufferedOutputStream bos = new BufferedOutputStream(fos);
                     byte[] bytes = new byte[1024];
                     int len=-1;
                     while ((len=zis.read(bytes))!=-1){
                         bos.write(bytes,0,len);
                     }
                     bos.close();
            }
            System.out.println(file.getAbsolutePath()+"文件解压成功");
                     zis.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
